package com.webssky.jcseg.lucence;

import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.Analyzer.TokenStreamComponents;

import com.webssky.jcseg.core.JCSegException;

/**
 * jcseg analyzer for lucene.
 * 
 * @author	chenxin <br />
 * @email	chenxin619315@gmail.com <br />
 * {@link http://www.webssky.com}
 */
public class JcsegAnalyzer extends Analyzer {
	
	private int mode;
	
	public JcsegAnalyzer( int mode ) {
		this.mode = mode;
	}

//	@Override
//	public TokenStream tokenStream(String fieldName, Reader reader) {
//		try {
//			return new JcsegTokenizer(mode, reader);
//		} catch ( Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
	
	/*
	 * 重载Analyzer接口，构造分词组件
	 */
	@Override
	protected TokenStreamComponents createComponents(String fieldName, final Reader in) {
		Tokenizer _IKTokenizer = null;
		try {
			_IKTokenizer = new JcsegTokenizer(mode , in);

		
		} catch (JCSegException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new TokenStreamComponents(_IKTokenizer);
	}
	
	
}
